package com.lzh.shijicloudstack.controller;

import com.lzh.shijicloudstack.common.R;
import com.lzh.shijicloudstack.common.ResultUtil;
import com.lzh.shijicloudstack.model.dto.calendarRecord.AddEventDto;
import com.lzh.shijicloudstack.model.dto.calendarRecord.DateRangeDto;
import com.lzh.shijicloudstack.model.dto.calendarRecord.EditEventDto;
import com.lzh.shijicloudstack.model.entity.CalendarRecord;
import com.lzh.shijicloudstack.model.vo.calendarRecord.CalendarRecordVO;
import com.lzh.shijicloudstack.service.CalendarRecordService;
import com.lzh.shijicloudstack.utils.SecurityUtils;
import com.lzh.shijicloudstack.utils.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 菜单日历记录模块
 *
 * @author luzhiheng
 * @date 2025-04-07
 */
@Api(tags = "菜单日历记录")
@RestController
@RequestMapping("/calendar/record")
public class CalendarRecordController {

    @Resource
    private CalendarRecordService calendarRecordService;

    @Resource
    private SqlUtil sqlUtil;

    @ApiOperation(value = "查询指定日期范围的菜单记录")
    @PostMapping("/findCookbookByRangeDate")
    public R<List<CalendarRecordVO>> findCookbookByRangeDate(@RequestBody DateRangeDto dto) {
        return ResultUtil.success(calendarRecordService.findCookbookByRangeDate(dto));
    }

    @ApiOperation(value = "删除分类")
    @PostMapping("/deleteEvent/{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        CalendarRecord calendarRecord = calendarRecordService.getById(id);
        calendarRecord.setOpenFlag(0);
        calendarRecord.setDeleteTime(new Date());
        calendarRecord.setDeleteUserId(String.valueOf(SecurityUtils.getUserId()));
        calendarRecord.setDeleteName(SecurityUtils.getUsername());
        sqlUtil.executeUpdateSql(sqlUtil.getUpdateSql(calendarRecord));
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "添加日程")
    @PostMapping("/addEvent")
    public R<Boolean> addEvent(@RequestBody @Valid AddEventDto dto) {
        return ResultUtil.success(calendarRecordService.addEvent(dto));
    }

    @ApiOperation(value = "修改日程")
    @PostMapping("/editEvent")
    public R<Boolean> editEvent(@RequestBody @Valid EditEventDto dto) {
        return ResultUtil.success(calendarRecordService.editEvent(dto));
    }

}
